﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace AlgoTester
{
    class FileOperator
    {

        private Dictionary<string, int> monthInt;

        public FileOperator()
        {
            monthInt = new Dictionary<string, int>();
            monthInt.Add("Jan", 1);
            monthInt.Add("Feb", 2);
            monthInt.Add("Mar", 3);
            monthInt.Add("Apr", 4);
            monthInt.Add("May", 5);
            monthInt.Add("Jun", 6);
            monthInt.Add("Jul", 7);
            monthInt.Add("Aug", 8);
            monthInt.Add("Sep", 9);
            monthInt.Add("Oct", 10);
            monthInt.Add("Nov", 11);
            monthInt.Add("Dec", 12);
        }

        // Called by Stockmarket to retrieve the names of stocks it should initialize
        public List<string[]> getStockList(string marketName)
        {
            List<string[]> returnList = new List<string[]>();
            // Retrieve file
            string filename = marketName + "_stock_list.txt";
            StreamReader inStream = getReadStream(filename);

            // Handling for if inStream wasn't found correctly
            if (inStream == null)
            {
                Console.WriteLine("READSTREAM NULL FOR " + filename);
                return null;
            }

            // Create returnLists 
            while (!inStream.EndOfStream)
            {
                string nextLine = inStream.ReadLine();
                returnList.Add(nextLine.Split());
            }
            return returnList;
        }

        // Returns a List of Stockquotes for the stock associated with ticker
        // Assumes a CSV file of data with Date,Open,Close,High,Low,Volume
        public List<StockQuote> getStockInfo(string ticker)
        {
            List<StockQuote> returnList = new List<StockQuote>();
            // Create filename and get Readstream
            String filename = ticker + "_stock_info.txt";
            StreamReader stockStream = getReadStream(filename);
            // Create StockQuotes and add them to returnList
            while (!stockStream.EndOfStream)
            {
                returnList.Add(cleanUpQuoteData(stockStream.ReadLine()));
            }
            return returnList;
        }

        // Helper for getStockInfo method
        // Takes raw data and return 
        private StockQuote cleanUpQuoteData(string arg)
        {
            string[] splitLine = arg.Split(',');
            string[] splitDate = splitLine[0].Split('-');
            int day = Convert.ToInt32(splitDate[0]);
            int month;
            monthInt.TryGetValue(splitDate[1], out month);
            int year = Convert.ToInt32(splitDate[2]);
            Date date = new Date(day, month, year);
            double open = Convert.ToDouble(splitLine[1]);
            double close = Convert.ToDouble(splitLine[2]);
            double high = Convert.ToDouble(splitLine[3]);
            double low = Convert.ToDouble(splitLine[4]);
            int volume = Convert.ToInt32(splitLine[5]);
            return new StockQuote(date, open, close, high, low, volume);
        }

        // Opens a file and returns StreamReader for it
        // Returns null if file not found
        private StreamReader getReadStream(string filename)
        {
            StreamReader sr = null;
            // Handling for if File isn't found
            if (File.Exists(filename))
                sr = File.OpenText(filename);
            else
                Console.WriteLine(filename + " NOT FOUND");
            return sr;
        }

    }
}
